package com.ruoyi.web.controller.expert;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.expert.mapper.FileMapper;
import com.ruoyi.expert.service.FileService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Author 程家伟
 * Date 2022/3/1 20:02
 * Email 3497357686@qq.com
 */

@CrossOrigin
@RestController
@RequestMapping("/expert")
public class FillController {
    @Autowired
    private FileService fileService;


    @PostMapping("/imagefile/firstupload")
    public AjaxResult imageUpload(int id,String from,@RequestParam("file") MultipartFile[] file){
        boolean upload = false;
        for (MultipartFile multipartFile : file) {
            upload = fileService.upload(id, from, multipartFile);
        }

        if (upload){
            return AjaxResult.success("图片上传成功");
        }else return AjaxResult.error("图片上传失败");
    }

    @PostMapping("/wordfile/firstupload")
    public AjaxResult wordUpload(int id , String from,@RequestParam("file") MultipartFile file){
        boolean upload = fileService.upload(id, from, file);
        if (upload){
            return AjaxResult.success("文档上传成功");
        }else return AjaxResult.error("文档上传失败");
    }

    @PostMapping("/imagefile/upload")
    public AjaxResult imageManyUpload(int id,String from,@RequestParam("file") MultipartFile[] file){
        boolean upload = false;
        fileService.deleteImage(id, from);
        for (MultipartFile multipartFile : file) {
            upload = fileService.upload(id, from, multipartFile);
        }

        if (upload){
            return AjaxResult.success("图片上传成功");
        }else return AjaxResult.error("图片上传失败");
    }

    @PostMapping("/wordfile/upload")
    public AjaxResult wordManyUpload(int id , String from,@RequestParam("file") MultipartFile file){
        fileService.deleteWordFile(id, from);
        boolean upload = fileService.upload(id, from, file);
        if (upload){
            return AjaxResult.success("文档上传成功");
        }else return AjaxResult.error("文档上传失败");
    }

    @GetMapping("/file/download")
    public void downloadFile(HttpServletRequest request,HttpServletResponse response){
        fileService.downloadFile(request,response);
    }

    @GetMapping("/file/viewpicture")
    public AjaxResult viewPicture(HttpServletResponse response,HttpServletRequest request){
        List<String> list = fileService.viewPicture(request, response);
        List<String> typeLiat = fileService.getTypeLiat(request);
        if (list !=null && typeLiat !=null){
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("content",list);
            ajaxResult.put("type",typeLiat);
            return ajaxResult;
        }else return AjaxResult.error();
    }

    @GetMapping("/file/worddelete")
    public AjaxResult deleteWord(int id,String from){
        boolean b = fileService.deleteWordFile(id, from);
        if (b){
            return AjaxResult.success("删除成功");
        }else return AjaxResult.error("删除失败");
    }

}
